//串口相关的头文件
#include <stdio.h>  /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "nativeUartFun.h"
#include "public.h"



#define TABLEN 2
const MCU_MONITOROBJ_TAB mcuMonitorObj_tbl[TABLEN] = {
	//len   oid    rwflag   opt2
	{0x05, 0x0001, 0x00, 0x00},
	{0x05, 0x0002, 0x01, 0x00}
	//	{0x05, 0x0003, 0x01, 0x00}
};
MCU_MONITOROBJ_VAL mcuMonitorObjVal[TABLEN];
INT16U search_nativeMcuTab_idx(INT16U oid);

INT16U nativeUartSetTreat(void)
{
	COM_TYPE *pCom;
	INT16U i, j, pkLen, calCrc, recCrc;
	INT8U oidLen;
	//INT8U *ptr;
	UNION_2_BYTE tmp;

	int temp = 0;
	for (i = 0; i < IO_DO_MAXLEN; i++)
	{
		if (0 != g_IOoutput[i].value)
			temp |= 1 << i;
	}
	i = search_nativeMcuTab_idx(IO_DO_OID);
	if (i > IO_DO_MAXLEN)
		return;
	mcuMonitorObjVal[i].serVal[0] = temp;
	mcuMonitorObjVal[i].serVal[1] = temp >> 8;

	pkLen = PACKET_HEAD_LEN;
	INT8U *ptr = &g_nativeUartSendBuf[PACKET_HEAD_LEN];
	for (i = 0; i < TABLEN; i++)
	{
		if (mcuMonitorObj_tbl[i].rwFlag != 1)
			continue;
		int rc = memcmp(&mcuMonitorObjVal[i].devVal[0], &mcuMonitorObjVal[i].serVal[0], 4);
		
		if (rc !=0)
		{
			*ptr++ = mcuMonitorObj_tbl[i].ol;
			*ptr++ = mcuMonitorObj_tbl[i].oid;
			*ptr++ = mcuMonitorObj_tbl[i].oid >> 8;
			memcpy(ptr, &mcuMonitorObjVal[i].serVal[0], (mcuMonitorObj_tbl[i].ol - 3));
			ptr += mcuMonitorObj_tbl[i].ol - 3;
			pkLen += mcuMonitorObj_tbl[i].ol;
		}
	}
	if (pkLen > PACKET_HEAD_LEN)
	{
		pCom = (COM_TYPE *)&g_nativeUartSendBuf[0];
		pCom->head.startFlag = mCmd_startFlag;
		pCom->head.packetCmd = mCmd_setData;
		pCom->head.packetAnswer = 0XFF;
		pCom->head.devId = 0X00;
		pCom->head.packetLength[0] = pkLen - 3;
		pCom->buffer[pkLen++] = 0x12;
		pCom->buffer[pkLen++] = 0x34;
		pCom->buffer[pkLen++] = mCmd_endFlag;
		return pkLen;
	}
	return 0;
}

INT16U nativeUartReadTreat(char *recvBuf, INT16U recLen)
{
	COM_TYPE *pCom;
	INT16U i, j, pkLen, calCrc, recCrc;
	INT8U oidLen;
	INT8U *ptr;
	UNION_2_BYTE tmp;

	DebugPrint("\nrecieve mcu packet:");
	for (i = 0; i < recLen; i++)
	{
		g_nativeUartSendBuf[i] = recvBuf[i];
		DebugPrint("%x ", recvBuf[i]);
	}
    DebugPrint("\n");
	

	pCom = (COM_TYPE *)g_nativeUartSendBuf;
	pkLen = pCom->head.packetLength[HIGH_ORDER] * 256 + pCom->head.packetLength[LOW_ORDER];


	if (recLen != pkLen + 6)goto end;
		
	if (pCom->head.devId == 0)
	{
		if (pCom->head.packetCmd == mCmd_getData || pCom->head.packetCmd == mCmd_setData || pCom->head.packetCmd == mCmd_report) //Ö÷»ú·¢ËÍÀ´µÄ²éÑ¯ÉèÖÃÍ¨ÐÅ°ü
		{
			ptr = &g_nativeUartSendBuf[PACKET_HEAD_LEN];
			for (i = 0; i < (pkLen - PACKET_HEAD_LEN + 3); i += oidLen)
			{
				oidLen = *ptr++;
				tmp.byte[0] = *ptr++;
				tmp.byte[1] = *ptr++;

				j = search_nativeMcuTab_idx(tmp.word);
				if (j != 0xffff)
				{
					if (oidLen != mcuMonitorObj_tbl[j].ol)
						goto end;
					if (pCom->head.packetCmd == mCmd_getData && pCom->head.packetAnswer == mRsp_success)
					{
						memcpy(&mcuMonitorObjVal[j].devVal[0], ptr, (oidLen - 3));
						ptr += oidLen - 3;
					}
					else if (pCom->head.packetCmd == mCmd_setData && pCom->head.packetAnswer == mRsp_success)
					{
						memcpy(&mcuMonitorObjVal[j].devVal[0], ptr, (oidLen - 3));
						mcuMonitorObjVal[j].setFlag = SET_FLAG;
						ptr += oidLen - 3;
					}
					else if (pCom->head.packetCmd == mCmd_report && pCom->head.packetAnswer == 0xff)
					{
						memcpy(&mcuMonitorObjVal[j].devVal[0], ptr, (oidLen - 3));
						if(mcuMonitorObj_tbl[j].rwFlag==1)
						{
							memcpy(ptr, &mcuMonitorObjVal[j].serVal[0], (oidLen - 3));
						}
						mcuMonitorObjVal[j].setFlag = SET_FLAG;
						ptr += oidLen - 3;
					}
					else
					{
						goto end;
					}
				}
				else
				{
					goto end;
				}
			}
			if (pCom->head.packetCmd == mCmd_getData || pCom->head.packetCmd == mCmd_setData)
				goto end;
		}
		else
		{
			goto end;
		}
	}
	else 
	{
		DebugPrint("\nrecieve BLE data:");
		for (i = 8; i < recLen-3; i++)DebugPrint("%c ", recvBuf[i]);
		DebugPrint("\n");
		goto end;
	}
	pCom->head.packetAnswer = mRsp_success;
	return recLen;
end:
	return 0;
}
INT16U search_nativeMcuTab_idx(INT16U oid)
{
	INT16U i, index;

	index = 0xffff;
	for (i = 0; i < TABLEN; i++)
	{
		if (mcuMonitorObj_tbl[i].oid == oid)
		{
			return i;
		}
	}
	return index;
}